package cn.stylefeng.guns.modular.api;

import cn.stylefeng.guns.config.redis.redis.util.Redishandler0;
import cn.stylefeng.guns.modular.business.common.Constants;
import cn.stylefeng.guns.modular.business.entity.MessageNotice;
import cn.stylefeng.guns.modular.business.model.result.MessageNoticeResult;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;


@RestController
@RequestMapping("/gunsApi/message")
public class MessageNoticeApi extends BaseController {


    @Autowired
    Redishandler0 redishandler0;

    @ApiOperation("根据类型分页获取消息")
    @GetMapping("/notice")
    public ResponseData getNoticeByType(@RequestHeader(value = "userId") Long userId,
                                        @RequestParam String type, @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                        @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        LocalDateTime today = LocalDateTime.now();
        long end = today.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        LocalDateTime twoMonthBefore = today.minusMonths(2);
        long start = twoMonthBefore.toInstant(ZoneOffset.of("+8")).toEpochMilli();
        pageNo = pageNo > 0 ? (pageNo - 1) * pageSize : 0;
        Page<MessageNoticeResult> page = new Page<>(pageNo, pageSize);
        Set<String> list;
        List<MessageNotice> notices;
        List<MessageNoticeResult> messageNoticeResults = new ArrayList<>();
        long total = 0;
        if (type.equals("0")) {
            list = redishandler0
                    .getZSet(Constants.NOTICE_KEY,
                            "", start, end, pageNo, pageSize);
            total = redishandler0
                    .getZSetCount(Constants.NOTICE_KEY, "", start, end);
            if (!CollectionUtils.isEmpty(list)) {
                notices = JSONArray.parseArray(list.toString(),
                        MessageNotice.class);
                messageNoticeResults = copy(notices);
            }
        } else if (type.equals("1")) {
            list = redishandler0
                    .getZSet(Constants.SYSTEM_INFO_KEY,
                            userId.toString(), start, end, pageNo, pageSize);
            messageNoticeResults = getMessageNoticeResults(type, userId, list, messageNoticeResults);
            total = redishandler0
                    .getZSetCount(Constants.SYSTEM_INFO_KEY, userId.toString(), start, end);
        } else if (type.equals("2")) {
            list = redishandler0
                    .getZSet(Constants.PLATFORM_NOTICE_KEY,
                            "", start, end, pageNo, pageSize);
            total = redishandler0
                    .getZSetCount(Constants.PLATFORM_NOTICE_KEY, "", start, end);
            messageNoticeResults = getMessageNoticeResults(type, userId, list, messageNoticeResults);
        }
        page.setTotal(Integer.valueOf(String.valueOf(total)));
        page.setRecords(messageNoticeResults);
        return ResponseData.success(page);
    }

    private List<MessageNoticeResult> getMessageNoticeResults(@RequestParam String type, Long userId, Set<String> list, List<MessageNoticeResult> messageNoticeResults) {
        List<MessageNotice> notices;
        if (!CollectionUtils.isEmpty(list)) {
            notices = JSONArray.parseArray(list.toString(),
                    MessageNotice.class);
            Set<String> ids = redishandler0.getSet(Constants.READ_MESSAGE_KEY, userId.toString() + type);
            messageNoticeResults = copy(notices);
            messageNoticeResults.forEach(messageNoticeResult -> {
                if (ids != null && ids.contains(messageNoticeResult.getId())) {
                    messageNoticeResult.setRead(true);
                } else {
                    messageNoticeResult.setRead(false);
                }
            });
        }
        return messageNoticeResults;
    }

    private List<MessageNoticeResult> copy(List<MessageNotice> notices) {
        List<MessageNoticeResult> messageNoticeResults = new ArrayList<>();
        notices.forEach(messageNotice -> {
            MessageNoticeResult messageNoticeResult = new MessageNoticeResult();
            BeanUtils.copyProperties(messageNotice, messageNoticeResult);
            messageNoticeResults.add(messageNoticeResult);
        });
        return messageNoticeResults;
    }


}


